<!--
 * @Description: 复制选择客户
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-08-28 20:10:19
 * @FilePath: /yuxiuproweb/src/views/mes/pro/workorder/copyClient/index.vue
-->
<template>
  <el-dialog
    :close-on-click-modal="false"
    v-dialogDrag
    title="复制订单"
    :visible.sync="shows"
    width="800px"
    append-to-body
    v-loading="copyLoading"
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="客户名称" prop="workorderCode">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getList"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="mb10" v-if="companyRow">
      已选择：{{ companyRow.clientName }}
    </div>
    <el-table
      v-loading="loading"
      :data="clientList"
      max-height="400"
      @row-click="checkedModelChange"
    >
      <el-table-column width="60" align="center">
        <template slot-scope="{ row }">
          <el-radio
            v-model="row.checked"
            class="radio-view"
            :label="true"
            @change="checkedModelChange(row)"
            ><i></i
          ></el-radio>
        </template>
      </el-table-column>
      <el-table-column align="center" type="index"></el-table-column>
      <el-table-column
        label="客户编码"
        align="center"
        prop="clientCode"
      ></el-table-column>
      <el-table-column
        label="客户名称"
        width="150px"
        align="center"
        prop="clientName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="客户简称" align="center" prop="clientNick" />
      <el-table-column label="客户电话" align="center" prop="tel" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { listClient } from "@/api/mes/md/client";
import { copyOrder } from "@/api/asc/order";
export default {
  props: {
    show: Boolean,
    data: {
      type: Object,
    },
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        clientName: null,
      },
      // 总条数
      total: 0,
      loading: false,
      copyLoading: false,
      clientList: [],
      companyRow: null,
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    resetQuery() {
      this.queryParams.clientName = "";
      this.queryParams.pageNum = 1;
      this.getList();
    },
    checkedModelChange(row) {
      this.clientList?.forEach((e) => {
        if (e.subIndex !== row.subIndex) e.checked = false;
        else e.checked = true;
      });
      this.companyRow = this.clientList?.find((e) => e.checked);
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listClient(this.queryParams).then((response) => {
        this.clientList = response.rows?.map((e, index) => {
          return {
            ...e,
            subIndex: index,
            checked: this.companyRow?.clientId === e.clientId,
          };
        });

        this.total = response.total;
        this.loading = false;
      });
    },
    async submitForm() {
      if (!this.companyRow) {
        this.$message.error("请选择客户");
        return;
      }
      this.copyLoading = true;
      copyOrder({
        orderId: this.data.proOrderId,
        clintId: this.companyRow.clientId,
      }).then((res) => {
        this.$modal.msgSuccess("复制成功");
        this.shows = false;
        this.copyLoading = false;
        this.$emit("on-change", res?.data);
      });
    },
  },
};
</script>
